#include "Sales_data.h"
#include <iostream>

Sales_data::Sales_data(std::istream &is) {
    read(is, *this);
}

double Sales_data::avg_price() const {
    if (this->units_sold) {
        return this->revenue / this->units_sold;
    } else {
        return 0;
    }
}

Sales_data& Sales_data::combine(const Sales_data &rhs) {
    units_sold += rhs.units_sold;
    revenue += rhs.revenue;
    return *this;
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs) {
    Sales_data sum = lhs;   // 把 lhs 的数据成员拷贝给 sum
    sum.combine(rhs);       // 把 rhs 的数据成员加到 sum 中
    return sum;
}

std::istream& read (std::istream& is, Sales_data &item)
{
    double price = 0;
    is >> item.bookNo >> item.units_sold >> price;
    item.revenue = price * item.units_sold;
    return is;
}

std::ostream& print (std::ostream& os, const Sales_data &item) {
    os << item.isbn() << " " << item.units_sold << " "
       << item.revenue << " " << item.avg_price();
    return os;
}